package GUI;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import core.model.Provincia;
import core.model.Usuario;

import GUI.manager.VentanaFactory;
import GUI.manager.VentanaManager;

public class panelRegistro extends javax.swing.JPanel implements ActionListener {
	
	public static int provincia;
	VentanaManager ventanaManager = VentanaFactory.getInstance();
	
	
	public panelRegistro() {
		initComponents();

	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 6057938321344962237L;
	private static final int INFORMATION_MESSAGE = 0;
	private static final int OK_CANCEL_OPTION = 0;
	private static final int ERROR_MESSAGE = 0;
	private static final int PLAIN_MESSAGE = 0;
	private javax.swing.JButton bAceptar;
	private javax.swing.JButton bLeerMas;
	private javax.swing.JButton bMapa;
	private javax.swing.JButton bTitulo;
	private javax.swing.JButton bAtras;
	private javax.swing.JButton bSalir;
	private javax.swing.JLabel etiquetaApellido1;
	private javax.swing.JLabel etiquetaApellido2;
	private javax.swing.JLabel etiquetaEmail;
	private javax.swing.JLabel etiquetaNick;
	private javax.swing.JLabel etiquetaNombre;
	private javax.swing.JLabel etiquetaPassword;
	private javax.swing.JLabel etiquetaPassword2;
	private javax.swing.JLabel etiquetaProvincia;
	private javax.swing.JRadioButton jRadioButton1;
	private javax.swing.JPasswordField pfPassword;
	private javax.swing.JPasswordField pfPassword2;
	private javax.swing.JTextField tfApellido;
	private javax.swing.JTextField tfApellido2;
	private javax.swing.JTextField tfEmail;
	private javax.swing.JTextField tfNick;
	private javax.swing.JTextField tfNombre;
	private JComboBox cbProvincia;
	private JFrame actual;
	private JPanel estePanel = this;
	private Usuario user = new Usuario();
	private HashMap<String,String> usuarios=ventanaManager.getPasswords();

	List<Provincia> provincias = ventanaManager.getAllProvincias();
	

	private void initComponents() {

		etiquetaNombre = new javax.swing.JLabel();
		etiquetaApellido1 = new javax.swing.JLabel();
		etiquetaApellido2 = new javax.swing.JLabel();
		etiquetaNick = new javax.swing.JLabel();
		etiquetaEmail = new javax.swing.JLabel();
		etiquetaProvincia = new javax.swing.JLabel();
		etiquetaPassword = new javax.swing.JLabel();
		etiquetaPassword2 = new javax.swing.JLabel();
		jRadioButton1 = new javax.swing.JRadioButton();
		bAceptar = new javax.swing.JButton();
		bMapa = new javax.swing.JButton();
		tfApellido = new javax.swing.JTextField();
		tfNombre = new javax.swing.JTextField();
		tfNick = new javax.swing.JTextField();
		tfApellido2 = new javax.swing.JTextField();
		tfEmail = new javax.swing.JTextField();
		cbProvincia = new JComboBox<String>(provinciasArray(provincias));
		pfPassword = new javax.swing.JPasswordField();
		pfPassword2 = new javax.swing.JPasswordField();
		bTitulo = new javax.swing.JButton();
		bLeerMas = new javax.swing.JButton();
		bAtras = new javax.swing.JButton();
		bSalir = new javax.swing.JButton();
		setBackground(new java.awt.Color(255, 255, 255));
		setPreferredSize(new java.awt.Dimension(610, 643));

		etiquetaNombre.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaNombre.setForeground(new java.awt.Color(243, 15, 120));
		etiquetaNombre.setText("NOMBRE");

		etiquetaApellido1
				.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaApellido1.setForeground(new java.awt.Color(243, 15, 120));
		etiquetaApellido1.setText("APELLIDO2");

		etiquetaApellido2
				.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaApellido2.setForeground(new java.awt.Color(243, 15, 120));
		etiquetaApellido2.setText("APELLIDO 1");

		etiquetaNick.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaNick.setForeground(new java.awt.Color(243, 15, 120));
		etiquetaNick.setText("NICK");

		etiquetaEmail.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaEmail.setForeground(new java.awt.Color(243, 15, 120));
		etiquetaEmail.setText("E-MAIL");

		etiquetaProvincia
				.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaProvincia.setForeground(new java.awt.Color(243, 15, 120));
		etiquetaProvincia.setText("PROVINCIA");

		etiquetaPassword
				.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaPassword.setForeground(new java.awt.Color(243, 15, 120));
		etiquetaPassword.setText("PASSWORD");

		etiquetaPassword2
				.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		etiquetaPassword2.setForeground(new java.awt.Color(243, 15, 120));
		etiquetaPassword2.setText("R. PASSWORD");

		jRadioButton1.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		jRadioButton1.setForeground(new java.awt.Color(73, 73, 73));
		jRadioButton1.setText("Acepto los terminos de uso");
		jRadioButton1.setBackground(new java.awt.Color(255, 255, 255));

		bAceptar.setBackground(new java.awt.Color(255, 255, 255));
		bAceptar.setFont(new java.awt.Font("Lucida Handwriting", 1, 14)); // NOI18N
		bAceptar.setForeground(new java.awt.Color(243, 15, 120));
		bAceptar.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botoneraAceptar.png")); // NOI18N
		bAceptar.setBorder(null);
		bAceptar.addActionListener(this);

		bMapa.setBackground(new java.awt.Color(255, 255, 255));
		bMapa.setFont(new java.awt.Font("Lucida Handwriting", 0, 12)); // NOI18N
		bMapa.setForeground(new java.awt.Color(243, 15, 120));
		bMapa.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/botonMapa.png"));
		bMapa.setBorder(null);
		bMapa.addActionListener(this);

		tfApellido.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
		tfApellido.setText(mapa.apellido1);

		tfNombre.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
		tfNombre.setText(mapa.nombre);

		tfNick.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
		tfNick.setText(mapa.nick);
		
		tfApellido2.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
		tfApellido2.setText(mapa.apellido2);

		tfEmail.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
		tfEmail.setText(mapa.email);

		cbProvincia.setFont(new java.awt.Font("Calibri", 0, 13)); // NOI18N
		cbProvincia.setSelectedIndex(provincia);

		bTitulo.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/encabezadoMain.png")); // NOI18N
		bTitulo.setBorder(null);
		bTitulo.setBorderPainted(false);
		bTitulo.addActionListener(this);
		bTitulo.setBackground(new java.awt.Color(255, 255, 255));

		bLeerMas.setFont(new java.awt.Font("Lucida Handwriting", 1, 13)); // NOI18N
		bLeerMas.setForeground(new java.awt.Color(73, 73, 73));
		bLeerMas.setText("( leer mas )");
		bLeerMas.setBorderPainted(false);
		bLeerMas.addActionListener(this);
		bLeerMas.setBackground(new java.awt.Color(255, 255, 255));

		bAtras.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/boton_atras.png")); // NOI18N
		bAtras.setBorder(null);
		bAtras.addActionListener(this);
		bAtras.setBackground(new java.awt.Color(255, 255, 255));

		bSalir.setIcon(new javax.swing.ImageIcon(
				"src/GUI/resources/menu-salir.png")); // NOI18N
		bSalir.setBorderPainted(false);
		bSalir.addActionListener(this);
		bSalir.setBackground(new java.awt.Color(255, 255, 255));

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(layout
										.createSequentialGroup()
										.add(37, 37, 37)
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.TRAILING)
												.add(etiquetaApellido2)
												.add(etiquetaApellido1)
												.add(etiquetaNick)
												.add(etiquetaEmail)
												.add(etiquetaProvincia)
												.add(etiquetaPassword)
												.add(etiquetaPassword2)
												.add(etiquetaNombre))
										.add(18, 18, 18)
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.LEADING,
														false)
												.add(cbProvincia)
												.add(tfNombre)
												.add(tfApellido)
												.add(tfApellido2)
												.add(tfNick)
												.add(tfEmail)
												.add(pfPassword)
												.add(pfPassword2,
														org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
														263, Short.MAX_VALUE))
										.add(40, 40, 40)
										.add(bMapa,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												65,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.add(layout
										.createSequentialGroup()
										.addContainerGap()
										.add(bTitulo,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												597,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.add(layout
										.createSequentialGroup()
										.add(92, 92, 92)
										.add(jRadioButton1)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(bLeerMas))
								.add(layout
										.createSequentialGroup()
										.addContainerGap()
										.add(bAtras)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(bSalir).add(97, 97, 97)
										.add(bAceptar)))
						.addContainerGap(
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(layout
						.createSequentialGroup()
						.addContainerGap()
						.add(bTitulo,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								121,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(tfNombre,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
								.add(etiquetaNombre))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(etiquetaApellido2)
								.add(tfApellido,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(etiquetaApellido1)
								.add(tfApellido2,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(etiquetaNick)
								.add(tfNick,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(etiquetaEmail)
								.add(tfEmail,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(etiquetaProvincia)
								.add(cbProvincia,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
								.add(bMapa))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(pfPassword,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
								.add(etiquetaPassword))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(etiquetaPassword2)
								.add(pfPassword2,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
						.add(18, 18, 18)
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.BASELINE)
								.add(jRadioButton1).add(bLeerMas))
						.add(layout
								.createParallelGroup(
										org.jdesktop.layout.GroupLayout.LEADING)
								.add(layout
										.createSequentialGroup()
										.add(18, 18, 18)
										.add(bAceptar,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												39,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.add(layout
										.createSequentialGroup()
										.add(16, 16, 16)
										.add(layout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.LEADING)
												.add(bAtras).add(bSalir))))));

	}// </editor-fold>

	public String[] provinciasArray(List<Provincia> provincias) {
		String[] arrayProvincias = new String[provincias.size()];
		for (int i = 0; i < provincias.size(); i++) {
			arrayProvincias[i] = provincias.get(i).getNombre();
		}
		return arrayProvincias;
	}

	/*
	 * public void registrarUsuario() { Usuario usuario = null; String nick =
	 * tfNick.getText(); String nombre = tfNombre.getText(); String apellido1 =
	 * tfApellido.getText(); String apellido2 = tfApellido2.getText(); String
	 * email = tfEmail.getText();
	 * 
	 * int codigo_provincia =
	 * ventanaManager.getCodigoProvincia((cbProvincia.getSelectedItem
	 * ().toString())); //poner provincia//cbProvincia.getSelectedIndex()
	 * 
	 * String password = null; if
	 * (pfPassword.getText().equals(pfPassword2.getText())) password =
	 * pfPassword.getText();
	 * 
	 * usuario = new Usuario(nick, nombre, apellido1, apellido2, email,
	 * codigo_provincia, password); ventanaManager.insertUser(usuario); }
	 */

	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == bTitulo) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelInicio());
			actual.setPreferredSize(new Dimension(610, 350));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}

		if (e.getSource() == bAtras) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelInicio());
			actual.setPreferredSize(new Dimension(610, 350));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bSalir) {
			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new panelSalir(3));
			actual.setPreferredSize(new Dimension(610, 330));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);

		}
		if (e.getSource() == bAceptar) {
			Icon icono = new ImageIcon("src/GUI/resources/error.gif");
			// registrarUsuario();
			if((tfNombre.getText().length()==0)||(tfNick.getText().length()==0)||(tfEmail.getText().length()==0)
					||(tfApellido.getText().length()==0)||(tfApellido.getText().length()==0)||(pfPassword.getText().length()==0)
					||(pfPassword.getText().length()==0)){
				
				JOptionPane.showOptionDialog(null, "No se han introducido todos los datos",
						"Registro incorrecto", OK_CANCEL_OPTION, PLAIN_MESSAGE, icono,
						new Object[] { "ACEPTAR" }, null);
				
				
			}else{
				if(!(pfPassword.getText().equals(pfPassword2.getText()))){
					
					JOptionPane.showOptionDialog(null, "Las passwords introducidas no coinciden",
							"Registro incorrecto", OK_CANCEL_OPTION, PLAIN_MESSAGE, icono,
							new Object[] { "ACEPTAR" }, null);
					
				}
				else{
					if(!(jRadioButton1.isSelected())){

						JOptionPane.showOptionDialog(null, "Se deben aceptar las condiciones de uso",
								"Registro incorrecto", OK_CANCEL_OPTION, PLAIN_MESSAGE, icono,
								new Object[] { "ACEPTAR" }, null);
					}else{
						if(usuarios.containsKey(tfNick))
						{
							JOptionPane.showOptionDialog(null, "El nick del usuario ya existe",
									"Registro incorrecto", OK_CANCEL_OPTION, PLAIN_MESSAGE, icono,
									new Object[] { "ACEPTAR" }, null);
						}
						
						else
						{
							Usuario usuario=new Usuario(tfNick.getText(), tfNombre.getText(), tfApellido.getText(), tfApellido2.getText(), tfEmail.getText(), cbProvincia.getSelectedIndex(), pfPassword.getText());
							HashMap <String,String> hashmap =ventanaManager.getPasswords();
							if(hashmap.containsKey(usuario))
							{
								//Excepcion usuario existe
							}
							else
							{
							
							ventanaManager.insertUser(usuario);
						Icon iconoAceptar = new ImageIcon("src/GUI/resources/aceptar.gif");
						JOptionPane.showOptionDialog(null, "Registro realizado con exito",
								"Registro nuevo", OK_CANCEL_OPTION, PLAIN_MESSAGE, iconoAceptar,
								new Object[] { "ACEPTAR" }, null);

						actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
						actual.getContentPane().remove(0);
						actual.getContentPane().add(new panelInicio());
						actual.setPreferredSize(new Dimension(610, 350));
						actual.pack();
						actual.repaint();
						UtilidadesGUI.CentrarPantalla(actual);
						}
						}
					}
					
				}
			}
			

		}
		if (e.getSource() == bMapa) {
			Usuario u=new Usuario();

			actual = (JFrame) UtilidadesGUI.getContenedorPrincipal(estePanel);
			actual.getContentPane().remove(0);
			actual.getContentPane().add(new mapa(u,1));
			actual.setPreferredSize(new Dimension(880, 710));
			actual.pack();
			actual.repaint();
			UtilidadesGUI.CentrarPantalla(actual);
			
			mapa.apellido1=tfApellido.getText();
			mapa.apellido2=tfApellido2.getText();
			mapa.email=tfEmail.getText();
			mapa.nick=tfNick.getText();
			mapa.nombre=tfNombre.getText();
		}

	}

}